//
//  BVConstants.m
//  Pods-BVIOS
//
//  Created by 崔晋 on 2019/8/11.
//

#import "BVConstants.h"
#import <sys/utsname.h>

static NSNumber * IS_IPHONEX_SERIERS = nil;

@implementation BVConstants

+ (BOOL)isIphoneXSeries {
    if ([IS_IPHONEX_SERIERS isKindOfClass:[NSNumber class]]) {
        return IS_IPHONEX_SERIERS.integerValue == 1;
    }
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString* platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    NSArray *iPhoneArray = @[@"iPhone10,3",
                             @"iPhone10,6",
                             @"iPhone11,2",
                             @"iPhone11,4",
                             @"iPhone11,6",
                             @"iPhone11,8"];
    NSArray* iphoneXSeries = [iPhoneArray copy];
    BOOL isIphoneXSeries = [iphoneXSeries containsObject:platform];
    NSArray* simulatorSeries = @[@"x86_64",@"i386"];
    if (!isIphoneXSeries && [simulatorSeries containsObject:platform]) {
        CGFloat height = [[UIScreen mainScreen] currentMode].size.height;
        NSArray* iphoneXSeriesHeight = @[@2436,@1792,@2688];
        for (NSInteger idx = 0; idx < [iphoneXSeriesHeight count]; idx++) {
            if (![iphoneXSeriesHeight[idx] isKindOfClass:[NSNumber class]]) {
                continue;
            }
            CGFloat value = [iphoneXSeriesHeight[idx] floatValue];
            if (fabs(height - value) < 0.01) {
                isIphoneXSeries = YES;
                break;
            }
        }
    }
    
    if (isIphoneXSeries) {
        IS_IPHONEX_SERIERS = @(1);
    } else {
        IS_IPHONEX_SERIERS = @(0);
    }
    
    return isIphoneXSeries;
}

+ (NSString *)userInfoFilePath {
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.txt"];
}

@end
